home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_pas
/
ootp_4
/
budget.pas
next >
Wrap
Pascal/Delphi Source File
|
1990-04-16
|
7KB
|
229 lines
program Budget1;
uses ListObj, Crt;
type
String15 = string[15];
String24 = string[24];
var
ParentName : String15;
Root : ListPtr;
type
BudgetPtr = ^BaseBudget;
BudItemPtr = ^BudItem;
BudCatPtr = ^BudCat;
BaseBudget = object( List ) { This object is the Trunk of the tree }
procedure Init;
procedure Report;
end;
BudCat = object( Node ) { This object is a Branch }
Category : String15;
Budgeted : real;
Actual : real;
Items : List;
Parent : ListPtr;
constructor Init( CatName : String15; HowMuchBudgeted : real );
function GetActual : real;
function GetVariance : real;
procedure Report;
end;
BudItem = object( Node ) { Objects of this type are Leaves }
Category : String15; { Name of the parent category }
ToWhom : String24;
ForWhat : String24;
HowMuch : real;
procedure Init( CatName : String15;
Who, What : String24;
Amount : real );
procedure Show;
end;
{$F+}
function FindBudCat( pNode : pointer ) : boolean;
{$F-}
var
pBudCat : ^BudCat;
begin
pBudCat := pNode;
if pBudCat^.Category = ParentName then
FindBudCat := true
else
FindBudCat := false;
end;
procedure BaseBudget.Init;
begin
List.Init;
Root := @Self;
end;
procedure BaseBudget.Report;
var
pBudCat : BudCatPtr;
Spent : real;
Budgeted : real;
begin
Spent := 0;
Budgeted := 0;
writeln;
writeln( 'BUDGET REPORT':40 );
if FindObject = true then { if there are members of BaseBudget }
repeat
pBudCat := GetCursor;
pBudCat^.Report;
Spent := Spent + pBudCat^.Actual;
Budgeted := Budgeted + pBudCat^.Budgeted;
until FindnextObject = false;
write( 'ALL CATEGORIES: Budgeted: $', Budgeted:4:2,
' Spent: $', Spent:4:2 );
if Budgeted >= Spent then
write( ' UNDER budget by $', (Budgeted - Spent):4:2 )
else
write( ' OVER budget by $', (Spent - Budgeted ):4:2 );
end;
procedure BudCat.Report;
var
pBudItem : BudItemPtr;
Header : string;
begin
Header := Category + '----------------------------------------' +
'---------------------------------------';
writeln( Copy( Header, 1, 79 ));
if Items.FindObject = true then
repeat
pBudItem := Items.GetCursor;
pBudItem^.Show;
until Items.FindNextObject = false;
writeln( 'Total spent in ', Category, ' budget category: $',
GetActual:4:2 );
if GetVariance > 0 then { over budget! }
writeln( 'You spent MORE than the budgeted $', Budgeted:4:2,
' by $', GetVariance:4:2 )
else
if GetVariance < 0 then { under budget !}
writeln( 'Hooray! You spent $', -GetVariance:4:2,
' LESS than the $', Budgeted:4:2,
' budgeted for this category.')
else
writeln( 'You''ve spent exactly the amount budgeted for the ',
Category, ' category.' );
writeln;
end;
constructor BudCat.Init( CatName : String15; HowMuchBudgeted : real );
var
code : integer;
SAns : string;
begin
Node.Init( SizeOf( Self ) );
Items.Init;
Category := CatName;
if HowMuchBudgeted > 0 then
Budgeted := HowMuchBudgeted
else
repeat
write( ' How much to budget for the new category ''',
CatName, '''? :' );
readln( SAns );
Val( SAns, Budgeted, code );
until code = 0;
Actual := -99.99;
Parent := Root;
AppendToList( Parent^ );
end;
function BudCat.GetActual : real;
var
pBudItem : BuditemPtr;
begin
if Items.FindObject = true then
begin
Actual := 0.0;
repeat
pBudItem := Items.GetCursor;
Actual := Actual + pBudItem^.HowMuch;
until Items.FindNextObject = false;
end
else
Actual := -99.99;
GetActual := Actual;
end;
function BudCat.GetVariance : real;
begin
GetVariance := GetActual - Budgeted;
end;
procedure BudItem.Show;
begin
writeln( ToWhom:32, '| ', ForWhat:32, '| $', HowMuch:4:2 );
end;
procedure BudItem.Init( CatName : String15;
Who, What : String24;
Amount : real );
var
pToParent : BudCatPtr;
Tmp : ListDemonType;
begin
Node.Init( SizeOf(Self) );
ToWhom := Who;
ForWhat := What;
Category := CatName;
HowMuch := Amount;
Tmp := Root^.FindObjectDemon;
Root^.FindObjectDemon := FindBudCat;
ParentName := Category;
if Root^.FindObject = true then
begin
pToParent := Root^.GetCursor;
AppendToList( pToParent^.Items );
end
else
begin
New( pToParent, Init( Category, -99.99 ) );
AppendToList( pToParent^.Items );
end;
Root^.FindObjectDemon := Tmp;
end;
var
MyBudget : BaseBudget;
Utilities, Entertainment, CreditPayment : BudCat;
Expense : array[0..10] of BudItem;
begin
ClrScr;
MyBudget.Init;
Utilities.Init( 'Utilities', 150.00 );
Entertainment.Init( 'Entertainment', 100.00 );
CreditPayment.Init( 'CreditPayment', 1000.00 );
{ all of these individual items could just as easily be obtained
from a file! }
Expense[0].Init( 'Utilities', 'Electric Co.', 'Electricity', 45.47 );
Expense[1].Init( 'Entertainment', 'Cinema 99', 'Movie', 12.50 );
Expense[2].Init( 'Utilities', 'Telco 2', 'Long distance', 56.12 );
Expense[3].Init( 'CreditPayment', 'AmEx', 'Travel expenses', 591.20 );
Expense[4].Init( 'CreditPayment', 'FirstBank', 'Car loan', 212.34 );
Expense[5].Init( 'Utilities', 'Telco 1', 'Phone service', 18.07 );
Expense[6].Init( 'Entertainment', 'Walton''s', 'SF books', 32.07 );
Expense[7].Init( 'Entertainment', 'Fish shop', 'Rental & Bait', 47.00 );
Expense[8].Init( 'CreditPayment', 'NextBank', 'Line of credit', 100.00 );
Expense[9].Init( 'Utilities', 'AAA Oil', 'Heating Oil', 37.09 );
Expense[10].Init( 'CreditPayment', 'LastBank', 'Computer', 96.46 );
{ Here's is the line that does all the work }
MyBudget.Report;
repeat until KeyPressed;
end.